package showtime.framework.algorithm.bts
{
	public class CompositeNode extends BehaviourNode
	{
		protected var childNodes:Vector.<BehaviourNode> = null;
		protected var currentChildNodeIndex:int = 0;
		
		public function CompositeNode()
		{
			childNodes = new Vector.<BehaviourNode>;
		}
		
		public function set childResult(value:Boolean):void
		{
			
		}
		
		public function addChildNode(node:BehaviourNode):void
		{
			childNodes.push(node);
			node.parentNode = this;
		}
		
		override public function execute():void
		{
			super.execute();
			currentChildNodeIndex = 0;
			executeChildNode();
		}
		
		public function executeChildNode():void
		{
			currentChildNode.execute();
		}
		
		protected function get currentChildNode():BehaviourNode
		{
			if (childNodes == null)
			{
				throw new Error("there is no child nodes in a composite Node");
			}
			
			if (currentChildNodeIndex >= childNodes.length)
			{
				return null;
			}
			
			return childNodes[currentChildNodeIndex];
		}
	}
}